<template>
  <div>
    <div class="node" v-has="{action: 'rate:map:list', effect: 'move'}">
    <div class="search_box topSelect">
      <div class="selectBox">
        <label>站点：</label>
        <el-select
        filterable
          v-model="searchs.siteId"
          clearable
          @change="getRate"
          placeholder="请选择"
        >
          <el-option  v-for="item of brandList"
            :key="item.id"
            :value="item.id"
            :label="item.name"></el-option>
        </el-select>
      </div>
      <div class="selectBox">
        <label>费率组：</label>
        <el-select
          v-model="searchs.rateGroupId"
          clearable
          @change="getRate"
          placeholder="请选择"
        >
          <el-option  v-for="item of rateList"
            :key="item.id"
            :value="item.id"
            :label="item.name"></el-option>
        </el-select>
      </div>
      <div class="searchBox1" style="margin-left: 20px">
        <el-button size="small" plain icon="el-icon-search" @click="getRate">查询</el-button>
      </div>
    </div>
    <Panel title="费率映射管理" :intro='intro' useBack>
      <div slot='ext' class="examine">
         <el-button v-has="{action: 'rate:map:add', effect: 'move'}" class="button-offt" type="success" icon="el-icon-plus" @click="()=>editStore()">新增</el-button>
    </div>
    <TableEncapsulation
      slot="body"
      :tableConfig="tableConfig"
      :tableData="tableData"
    >
    <template slot="operation" slot-scope="operation">
          <el-button size="mini" icon="el-icon-edit"
            v-has="{action: 'rate:map:update', effect: 'move'}"
            @click="()=>editStore(operation.row)"
          >编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete"
            v-has="{action: 'rate:map:delete', effect: 'move'}"
            @click="()=>remove(operation.row.id)"
          >删除</el-button>
        </template>
        <TablePager
          slot="pager"
          v-if="pager.use"
          :size="pager.size"
          :pageChange="pageChange"
          :show-total="pager.showTotal"
          :total="pager.total"
          :current="pager.current"
        >
          <span class="demonstration">GO></span>
        </TablePager>
    </TableEncapsulation>
    </Panel>
    <!-- 添加编辑 start-->
    <ModalPanel 
      :visible='visible'
      title="添加/编辑"
      :onCancel="onCancel"
      class="bannerDialog"
      :onOk="onSubmit">
      <div>
        <ModifyForm
          :hiddenBtn='true'
          :editColumn='editColumn'
          :updateData="updateData"
          labelWidth='100px'
          ref="configModal"
          class="ModifyForm"
          :temporaryData='modalData'>
        </ModifyForm>
      </div>
    </ModalPanel>
    <!-- 添加编辑end -->
    </div>
    <div style="display: none;width:100%;height:100%;margin: auto;" v-has="{action: 'rate:map:list', effect: 'noPremiss'}">
      <NoPremission></NoPremission>
    </div>
  </div>
</template>
<script>
import request from "@/tools/request";
import Panel from "@/components/Panel";
import TableEncapsulation from "@/components/TableEncapsulation";
import TablePager from "@/components/Pager";
import SearchInput from "@/components/SearchInput";
import { objectFilter } from "@/tools/array.js";
import ModifyForm from "@/components/ModifyForm";
import ModalPanel from "@/components/ModalPanel";
export default {
  name: "rate",
  components: {
    TableEncapsulation,
    TablePager,
    Panel,
    SearchInput,
    ModifyForm,
    ModalPanel
  },
  data() {
    return {
      intro:{
        title: '小程序费率映射',
        content:`<ul><li><strong>配置站点充电结算计费规则</strong></li><li><strong>一个站点对应一个计费规则</strong></li><li><strong>请注意上线前必须配置计费规则，否则场站不显示价格，也不能正常充电</strong></li></ul>`
      },
      searchs:{
        component: 'rate',
        name:'',
        siteId:"",
        rateGroupId:'',
      },
      tableData: [],
       pager: {
        component: 'rate',
        use: true,
        total: 0,
        current: 1,
        size: 10,
        showTotal: true
      },
      rateList: [{id:'',name:'请选择'}],
      operation: true,
      brandList:[{id:'',name:'请选择'}],
      //添加编辑
      edit: false,
      visible:false,
      modalData:{},
      editColumn: [
        {
          name: "站点",
          prop: "siteId",
          type: "select",
          optionBindLabel: 'name',
          optionBindValue: 'id',
          optionList: [{'id':'','name':'请选择站点'}],
          onChange: () => {},
          required:true,
          width:24
        },
        {
          name: "费率组",
          prop: "rateGroupId",
          type: "select",
          optionBindLabel: 'name',
          optionBindValue: 'id',
          optionList: [{'id':'','name':'请选择费率组'}],
          onChange: () => {},
          required:true,
          width:24
        },
      ],
      //添加编辑end
    };
  },
  created() {
    if(this.$route.query.id){
      this.searchs.siteId = this.$route.query.id
    }
    // this.searchsFnc()
    this.getRate()
    this.iotSite()
    this.rate()
  },
  computed: {
    tableConfig () {
      return {
        selection: true,
        columnConfig: [
          {
            label: "编号",
            type: "text",
            prop: "id",
            align: "center"
          },
          {
            label: "站点",
            type: "text",
            prop: "siteName",
            align: "center"
          },
          {
            label: "费率组",
            type: "text",
            prop: "rateGroupName",
            align: "center",
            onClick: (val) => this.viewGroup(val),
            style: 'color: blue;cursor: pointer',
          },
          this.operation && {
            type: "operation",
            align: "center",
            label: "操作",
            width:240
          }
        ].filter(Boolean)
        }
    }
  },
  methods: {
     viewGroup(val){
       this.$store.commit('saveSearchs', this.searchs)
      this.$store.commit('savePager', this.pager)
      this.$router.push({ path: "/grate/group/time",query:{id:val.rateGroupId, name: val.rateGroupName} })
     },
    //  searchsFnc (){
    //   let searchs = this.$store.getters.searchs
    //   if(searchs &&searchs.component == 'rate'){
    //     this.searchs = searchs
    //   }
    //   let pager = this.$store.getters.pager
    //   if(pager &&pager.component == 'rate'){
    //     this.pager = pager
    //   }
    // },
    async getRate() {
      let rateGroupId = this.searchs.rateGroupId, page = this.pager.current, siteId = this.searchs.siteId
      let seach = objectFilter({ rateGroupId, page, siteId})
      const res = await request({
        url: "/rate/map/list",
        method: "POST",
        data: seach
      });
      this.tableData = res.page.list
      this.pager.total = res.page.totalCount
      this.pager.current = res.page.currPage
      
    },
    pageChange(val) {
      this.pager.current = val;
      this.getRate();
    },
    changeSearch(key, val) {
      this[key] = val;
      this.pager.current = 1;
    },
    editStore(id){
      this.$store.commit('saveSearchs', this.searchs)
      this.$store.commit('savePager', this.pager)
      this.$router.push({ path: "/rate/map/info",query:{id:id} })
    },
    commentDetail(id){
      this.$router.push({ path: "/storeDetail",query:{id:id,type: "toview"} })
    },
    //获取站点
    async iotSite(){
      let { list } = await request({
      url: "/iot/site/select",
      method: "POST",
      data: {}
      });
      this.brandList = list
    },
    async rate(){
      let { list } = await request({
      url: "/grate/group/select",
      method: "POST",
      data: {}
      });
      this.rateList = list 
      this.editColumn[1].optionList = list
    },
    //添加编辑start
    editStore(rowData){
      if(rowData){
        this.modalData = {...rowData}
      }else{
        this.modalData ={}
      }
      this.iotSiteInfo(this.modalData.siteId)
      this.visible = true
    },
    async onSubmit(){
      this.$refs['configModal'].onSubmit()
    },
    onCancel(){
      this.visible = false
    },
    async updateData(values){
     let url, msg
     const { siteId, rateGroupId, id } = values
     if(id){
        url = '/rate/map/update'
        msg = '编辑成功'
     }else{
        url = '/rate/map/add'
        msg = '添加成功'
     }
      let res = await request({
        url: url,
        method: "POST",
        data: { siteId, rateGroupId, id }
      });
     if(res.code !==0) return
      this.$message.success(msg)
      this.visible = false
      this.getRate();
    },
    //站点
    async iotSiteInfo(id){
      let { list } = await request({
      url: "/iot/site/rate/no",
      method: "GET",
      data: {}
      });
      let info = {}
      if(id){
        let res = await request({
        url: "/iot/site/info/" + id,
        method: "GET",
        data: {}
        });
        info = res.info
      }
      if(info){
        this.editColumn[0].optionList = [...list, info]
      }else{
        this.editColumn[0].optionList = list
      }
    },
    //添加编辑end
    remove(id){
       this.$confirm('确认删除吗', '操作确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then( async () => {
        let del = await request({
        url: "/rate/map/delete",
        method: "POST",
        dataType4:'json',
        data: [id]
      });
      if(del.code !==0) return
        this.$message.success('删除成功')
        if(this.tableData.length===1 && this.pager.current>1)
          this.$set(this.pager,'current', this.pager.current-1)
        this.getRate()
      })
    },
  }
}
</script>
<style lang='scss' module>
.examine{
    display: flex;
    align-items: center;
    :global{
        div{
            color: #fff;
            padding: 10px;
            margin: 0 10px;
            border-radius: 2px;
            font-size: 14px;
            cursor: pointer;
            min-width: 60px;
            text-align: center;
        }
        .adopt{
                background: #409EFF;
        }
    }
}

</style>